<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/7/6
 * Time: 17:33
 */

namespace app\admin\helper;

use think\Config;
use think\Cookie;

class LangHelper
{

    public static function convertLang($content,$tag=''){
        $code = Cookie::get('curr_lang');
        $code = empty($code)?'zh':$code;
        if (is_array($content)) {
            return self::LangArray($content,$code,$tag);
        }else {
            return self::LangString($content,$code,$tag);
        }
    }

    /**
     * 多维数组字符串替换 （先转json 替换完 再转回数组）
     * @param $content
     * @param $code
     * @return mixed
     */
    private static function LangArray($content,$code,$tag){
        $content = json_encode($content);
        $content = self::LangString($content,$code,$tag);
        return json_decode($content,true);
    }

    /**
     * 字符串替换
     * @param $content
     * @param $code
     * @return mixed
     */
    private static function LangString($content,$code,$tag){
        if ($tag!='')
            $tag = $tag.'_';
        $lang = Config::get('lang_'.$tag.$code);
        foreach ($lang as $k=>$v) {
            $content = str_replace('__'.$k.'__',$v,$content);
        }
        return $content;
    }

}